package mappers;

import gui.LoginGUI;
import gui.MainWindowGUI;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JOptionPane;

import managers.DatabaseConnection;



public class Login {
	private String databaseUsername, databasePassword;
	
	public Login() {
		
		
	}
	
	public boolean checkLogin(String username, String password) {
		boolean login = true;
		String sql = "SELECT * FROM PPKEA.login where username = ? and password = ?";
		try {
		DatabaseConnection dbConnect = new DatabaseConnection();

		PreparedStatement query = dbConnect.getConnect().prepareStatement(sql);
		query.setString(1, username);
		query.setString(2, password);
		
		ResultSet rs = query.executeQuery();
		
		while(rs.next()) {
			databaseUsername = rs.getString("username");
			databasePassword = rs.getString("password");		
		}
		
		if(username.equals(databaseUsername) && password.equals(databasePassword)){			
			MainWindowGUI mWindow = new MainWindowGUI();
			login = true;
		} else {
			JOptionPane.showMessageDialog(null, "The username or password you entered" +
					" is incorrect. Please contact the administrator!");
			login = false;
		}
		
		dbConnect.getConnect().close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return login;
	}
}
